Conversation
|
|
||
| def to_tree(self, *args, level=0, **kwargs): | ||
| alias_str = f', alias={self.alias.to_tree()}' if self.alias else '' | ||
| return indent(level) + f'Identifier(parts={[str(i) for i in self.parts]}{alias_str})' | ||
| with_rollup_str = ', with_rollup=True' if self.with_rollup else '' | ||
| return indent(level) + f'Identifier(parts={[str(i) for i in self.parts]}{alias_str}{with_rollup_str})' | ||
|
|
||
| def get_string(self, *args, **kwargs): | ||
| return self.parts_to_str() |
There was a problem hiding this comment.
Correctness: The to_tree method uses self.alias.to_tree() without checking if self.alias has a to_tree method. This could lead to an AttributeError if self.alias is not None but lacks the method. Ensure self.alias is of a type that implements to_tree or add a check before calling it.
📝 Committable Code Suggestion
‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.
| def to_tree(self, *args, level=0, **kwargs): | |
| alias_str = f', alias={self.alias.to_tree()}' if self.alias else '' | |
| return indent(level) + f'Identifier(parts={[str(i) for i in self.parts]}{alias_str})' | |
| with_rollup_str = ', with_rollup=True' if self.with_rollup else '' | |
| return indent(level) + f'Identifier(parts={[str(i) for i in self.parts]}{alias_str}{with_rollup_str})' | |
| def get_string(self, *args, **kwargs): | |
| return self.parts_to_str() | |
| return '.'.join(self.iter_parts_str()) | |
| def to_tree(self, *args, level=0, **kwargs): | |
| alias_str = f', alias={self.alias.to_tree()}' if self.alias and hasattr(self.alias, 'to_tree') else '' | |
| with_rollup_str = ', with_rollup=True' if self.with_rollup else '' | |
| return indent(level) + f'Identifier(parts={[str(i) for i in self.parts]}{alias_str}{with_rollup_str})' | |
| def get_string(self, *args, **kwargs): | |
| return self.parts_to_str() | |
| ind = indent(level) | ||
| ind1 = indent(level+1) | ||
| category_str = f'{ind1}category={repr(self.category)},' | ||
| from_str = f'\n{ind1}from={self.from_table.to_string()},' if self.from_table else '' | ||
| in_str = f'\n{ind1}in={self.in_table.to_tree(level=level + 2)},' if self.in_table else '' | ||
| from_str = f'\n{ind1}from_table={self.from_table.to_tree(level=level + 2)},' if self.from_table else '' | ||
| in_str = f'\n{ind1}in_table={self.in_table.to_tree(level=level + 2)},' if self.in_table else '' | ||
| where_str = f'\n{ind1}where=\n{self.where.to_tree(level=level+2)},' if self.where else '' | ||
| name_str = f'\n{ind1}name={self.name},' if self.name else '' | ||
| like_str = f'\n{ind1}like={self.like},' if self.like else '' | ||
| modes_str = f'\n{ind1}modes=[{",".join(self.modes)}],' if self.modes else '' | ||
| name_str = f'\n{ind1}name={repr(self.name)},' if self.name else '' | ||
| like_str = f'\n{ind1}like={repr(self.like)},' if self.like else '' | ||
| modes_str = f'\n{ind1}modes=[{",".join([repr(m) for m in self.modes])}],' if self.modes else '' | ||
| out_str = f'{ind}Show(' \ | ||
| f'{category_str}' \ | ||
| f'{name_str}' \ |
There was a problem hiding this comment.
Correctness: The change from to_string() to to_tree() for from_table and in_table may lead to inconsistent string representations if to_tree() does not return a string. Ensure to_tree() returns a string or adjust the logic accordingly.
| from mindsdb_sql_parser import parse_sql | ||
| from mindsdb_sql_parser.ast import Select, Identifier, BinaryOperation, Star | ||
| from mindsdb_sql_parser.ast import Variable, Function | ||
| from mindsdb_sql_parser.parser import Show | ||
| from mindsdb_sql_parser.ast import Select, Identifier, BinaryOperation, Star, Variable, Function, ASTNode | ||
|
|
||
|
|
||
| def compare_ast(parsed: ASTNode, expected: ASTNode, sql: str) -> None: | ||
| assert parsed.to_tree() == expected.to_tree() | ||
| assert str(parsed).lower() == sql.lower() | ||
| assert str(parsed) == str(expected) | ||
| assert str(eval(parsed.to_tree())) == str(parsed) | ||
|
|
||
|
|
||
| class TestMySQLParser: |
There was a problem hiding this comment.
Correctness: The use of eval in assert str(eval(parsed.to_tree())) == str(parsed) can introduce security risks if parsed.to_tree() contains untrusted input. Consider using a safer alternative to evaluate or compare the tree structure.
📝 Committable Code Suggestion
‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.
| from mindsdb_sql_parser import parse_sql | |
| from mindsdb_sql_parser.ast import Select, Identifier, BinaryOperation, Star | |
| from mindsdb_sql_parser.ast import Variable, Function | |
| from mindsdb_sql_parser.parser import Show | |
| from mindsdb_sql_parser.ast import Select, Identifier, BinaryOperation, Star, Variable, Function, ASTNode | |
| def compare_ast(parsed: ASTNode, expected: ASTNode, sql: str) -> None: | |
| assert parsed.to_tree() == expected.to_tree() | |
| assert str(parsed).lower() == sql.lower() | |
| assert str(parsed) == str(expected) | |
| assert str(eval(parsed.to_tree())) == str(parsed) | |
| class TestMySQLParser: | |
| from mindsdb_sql_parser import parse_sql | |
| from mindsdb_sql_parser.parser import Show | |
| from mindsdb_sql_parser.ast import Select, Identifier, BinaryOperation, Star, Variable, Function, ASTNode | |
| def compare_ast(parsed: ASTNode, expected: ASTNode, sql: str) -> None: | |
| assert parsed.to_tree() == expected.to_tree() | |
| assert str(parsed).lower() == sql.lower() | |
| assert str(parsed) == str(expected) | |
| # Removed eval for security reasons | |
| assert parsed.to_tree() == expected.to_tree() | |
| class TestMySQLParser: | |
| pass # Placeholder for test methods |
| assert parsed.to_tree() == expected.to_tree() | ||
| assert str(parsed).lower() == sql.lower() | ||
| assert str(parsed) == str(expected) | ||
| assert str(eval(parsed.to_tree())) == str(parsed) |
to_treemethod for queries with ROLLUPto_treemethod forSHOWqueries